﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the User model
	/// </summary>
	public partial class User
	{

		// Flags used when loading a dictionary of users
		public enum eUserLoadFlags
		{
			FullName = 0,
			Abbreviated = 1,
			Username = 2,
			FullNameAndManagerSuffix = 3,
			AbbreviatedAndManagerSuffix = 4
		}

		/////////////////////////////////////////////////////////////
		// Attributes

		public Definitions.eRoles Roles
		{
			get { return (Definitions.eRoles)this._fdRoles; }
			set { this.fdRoles = (int)value; }
		}

		/////////////////////////////////////////////////////////////
		// Operations

		public bool Contains(Definitions.eRoles role)
		{

			// call sibling function
			return Definitions.Contains(this.Roles, role);

		}

		/////////////////////////////////////////////////////////////////
		// Static Operations

		public static string GetFullName( DebtChasedEntities ent, int iUserId )
		{

			// Declare return variable
			string strFtmp = string.Empty;

			// Load the users full-name
			var userDetails = (from u in ent.Users
								where u.UserId == iUserId
								select new { u.FinanceStaffContactId, u.ClientContactId, u.AccountContactId }).FirstOrDefault();
			if (userDetails != null)
			{

				// Compare contact ids
				if (userDetails.FinanceStaffContactId != null)
					strFtmp = (from fsc in ent.FinanceStaffContacts where fsc.FinanceStaffContactId == userDetails.FinanceStaffContactId select fsc.FullName).FirstOrDefault();
				else if (userDetails.ClientContactId != null)
					strFtmp = (from cc in ent.ClientContacts where cc.ClientContactId == userDetails.ClientContactId select cc.FullName).FirstOrDefault();
				else if (userDetails.AccountContactId != null)
					strFtmp = (from ac in ent.AccountContacts where ac.AccountContactId == userDetails.AccountContactId select ac.FullName).FirstOrDefault();

			}

			// Return variable
			return strFtmp;

		}

		public static User GetUserByUsername(DebtChasedEntities ent, string strUsername)
		{

			// Declare return variable
			return ent.Users.FirstOrDefault(u => u.Username == strUsername);

		}

		public static Definitions.eRoles GetUserRoles(DebtChasedEntities ent, string strUsername)
		{

			// Declare return variable
			int iFtmp = (from u in ent.Users
						 where (u.Username == strUsername)
						 select u.fdRoles).FirstOrDefault();

			// Return variable
			return (Definitions.eRoles)iFtmp;

		}

		public static Definitions.eRoles GetUserRoles(DebtChasedEntities ent, int iUserId)
		{

			// Declare return variable
			int iFtmp = (from u in ent.Users
						 where (u.UserId == iUserId)
						 select u.fdRoles).FirstOrDefault();

			// Return variable
			return (Definitions.eRoles)iFtmp;

		}

		public static bool DoesUserExist(DebtChasedEntities ent, string strUsername, string strPassword)
		{

			// Find the user in the table
			int iUserId = (from u in ent.Users
						   where ((u.Username == strUsername) && (u.Password == strPassword))
						   select u.UserId).FirstOrDefault();

			// Return variable
			return (iUserId != 0);

		}

		public static Dictionary<int, string> GetDictionary(DebtChasedEntities ent, Definitions.eRoles Roles, bool bAddNoneOption)
		{

			// Call stored-procedure
			var results = ent.UserDetailsProc((int)Roles);

			// Declare return variable
			Dictionary<int, string> Ftmp = null;
			if (bAddNoneOption)
			{
				Ftmp = new Dictionary<int, string>();
				Ftmp.Add(0, Definitions.None);
				foreach (var ob in results)
					Ftmp.Add(ob.fdUserId, ob.fdFullName);
			}
			else
				Ftmp = results.ToDictionary(u => u.fdUserId, u => u.fdFullName);

			// Return variable
			return Ftmp;

		}

	}

}
